package service

import (
	"fmt"
	"zhonghui/console/warehouse/cfgx"

	"github.com/ddliu/go-httpclient"
	jsoniter "github.com/json-iterator/go"
)

type GetActiveCodeOrderListRequest struct {
	Corpid string `json:"corpid"`
}
type GetActiveCodeOrderListItem struct {
	OrderID   string `json:"order_id"`
	OrderType int    `json:"order_type"`
}
type GetActiveCodeOrderListResponse struct {
	BaseResp
	HasMore    int                          `json:"has_more"`
	NextCursor string                       `json:"next_cursor"`
	OrderList  []GetActiveCodeOrderListItem `json:"order_list"`
}

func GetActiveCodeOrderList(providerAccessToken, corpid string) (*GetActiveCodeOrderListResponse, error) {
	resp := GetActiveCodeOrderListResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/license/list_order?provider_access_token=%s", host, providerAccessToken)

	jsonBody, err := jsoniter.MarshalToString(GetActiveCodeOrderListRequest{
		Corpid: corpid,
	})
	if err != nil {
		return nil, err
	}
	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}

type GetActiveCodeListRequest struct {
	Corpid  string `json:"corpid"`
	OrderID string `json:"order_id"`
}

type GetActiveCodeListItem struct {
	ActiveCode string `json:"active_code"`
	Type       int    `json:"type"`
	Userid     string `json:"userid"`
}

type GetActiveCodeListResponse struct {
	BaseResp
	AccountList []GetActiveCodeListItem `json:"account_list"`
	HasMore     int                     `json:"has_more"`
	NextCursor  string                  `json:"next_cursor"`
}

func GetActiveCodeList(providerAccessToken, OrderId, corpId string) (*GetActiveCodeListResponse, error) {
	resp := GetActiveCodeListResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/license/list_order_account?provider_access_token=%s", host, providerAccessToken)

	jsonBody, err := jsoniter.MarshalToString(GetActiveCodeListRequest{
		OrderID: OrderId,
		Corpid:  corpId,
	})
	if err != nil {
		return nil, err
	}
	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}

type ActiveUserRequest struct {
	ActiveCode string `json:"active_code"`
	Corpid     string `json:"corpid"`
	Userid     string `json:"userid"`
}

type ActiveUserResponse struct {
	BaseResp
}

func ActiveUser(providerAccessToken, CorpId, userId, activeCode string) (*ActiveUserResponse, error) {
	resp := ActiveUserResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/license/active_account?provider_access_token=%s", host, providerAccessToken)

	jsonBody, err := jsoniter.MarshalToString(ActiveUserRequest{
		ActiveCode: activeCode,
		Corpid:     CorpId,
		Userid:     userId,
	})
	if err != nil {
		return nil, err
	}

	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}

type GetUserActiveStatusResponse struct {
	BaseResp
	ActiveStatus int32 `json:"active_status"`
}

type GetUserActiveStatusRequest struct {
	CorpId string `json:"corpid"`
	UserId string `json:"userid"`
}

func GetUserActiveStatus(providerAccessToken, corpId, userId string) (*GetUserActiveStatusResponse, error) {
	resp := GetUserActiveStatusResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/license/get_active_info_by_user?provider_access_token=%s", host, providerAccessToken)
	jsonBody, err := jsoniter.MarshalToString(GetUserActiveStatusRequest{
		CorpId: corpId,
		UserId: userId,
	})
	if err != nil {
		return nil, err
	}
	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}
